package cn.com.haopy.yue.ai.wx.service;


import cn.com.haopy.yue.ai.core.config.wx.WxMaProperties;
import cn.com.haopy.yue.ai.core.enums.PayStatusCode;
import cn.com.haopy.yue.ai.core.system.SystemConfig;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskOrder;
import cn.com.haopy.yue.ai.db.entity.arisk.HOSUser;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskOrderService;
import cn.com.haopy.yue.ai.db.service.hos.HOSUserService;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Objects;

/**
 * 微信支付
 */
@Service
@Slf4j
public class WxPayCommService {

    @Autowired
    @Qualifier("wxMaPayService")
    private WxPayService wxMaPayService;

    @Autowired
    private WxMaProperties wxMaProperties;

    @Autowired
    private AIRiskOrderService orderService;

    @Autowired
    private HOSUserService userService;


    /**
     * @param orderNo
     * @param openid
     * @param body
     * @param orderFee
     * @param ip
     * @return
     */
    public Object wxPay(String orderNo, String openid, String body, BigDecimal orderFee, String ip) {

        WxPayMpOrderResult result = null;
        try {
            WxPayUnifiedOrderRequest orderRequest = new WxPayUnifiedOrderRequest();
            orderRequest.setOutTradeNo(orderNo);
            orderRequest.setOpenid(openid);
            orderRequest.setBody(body);
            // 元转成分
            int fee = 0;
            fee = orderFee.multiply(new BigDecimal(100)).intValue();
            orderRequest.setTotalFee(fee);
            orderRequest.setSpbillCreateIp(ip);

            log.info("支付返回：", orderRequest);

            result = wxMaPayService.createOrder(orderRequest);
            System.out.println("支付返回：" + result);


        } catch (Exception e) {
            log.error(e.toString());
            e.printStackTrace();
            return ResponseUtil.fail(e.getMessage());
        }
        return ResponseUtil.ok(result);


    }

    @Transactional
    public Boolean wxPayBack(String orderNo, String payId) {

        AIRiskOrder order = orderService.getByOrderNo(orderNo);
        if (Objects.isNull(order)) {
            return false;
        }

        if (order.getPayStatus().equals(PayStatusCode.PAYSUCCESS.getCode())) {
            return true;
        }

        //用户
        HOSUser hosUser = userService.getById(order.getUserId());
        if (Objects.isNull(hosUser)) {
            return false;
        }
        LocalDate endDate = hosUser.getVipEndDate();
        LocalDate now = LocalDate.now();
        if (Objects.isNull(endDate)) {
            endDate = now;
        }
        if (endDate.isBefore(now)) {
            endDate = now;
        }
        hosUser.setVipEndDate(endDate.plusMonths(SystemConfig.getVipchargeMonth()));
        userService.updateById(hosUser);


        //支付成功
        order.setPayTime(LocalDateTime.now());
        order.setPayId(payId);
        order.setPayStatus(PayStatusCode.PAYSUCCESS.getCode());


        return orderService.updateById(order);


    }


}
